import 'dart:ui';

import 'package:flutter_common_utils/common_utils_export.dart';
import 'package:flutter_live_data/live_data_export.dart';
import 'package:get/get.dart';
import '../../constant/event_bus_key.dart';
import '../../constant/sp_key.dart';
import '../../widgets/dialog/popup/popup_select.dart';
import 'i18n_zh_cn.dart';
import 'i18n_en_us.dart';

extension I18nStringExtension on String {
  String get trx => tr;
}

class I18nLocalName implements IShowText {
  final String name;
  final Locale locale;

  I18nLocalName(this.name, this.locale);

  @override
  String showText() => name;
}

// 简体中文 zh_CN
// 德语 de_DE
// 英语 en_US
class I18nConfig extends Translations {
  I18nConfig._();

  factory I18nConfig.create() = I18nConfig._;

  @override
  Map<String, Map<String, String>> get keys => {'zh_CN': i18n_zh_cn, 'en_US': i18n_en_us};

  static Locale localeEnUs = const Locale('en', 'US');
  static Locale localeZhCn = const Locale('zh', 'CN');

  //支持的语言
  static List<Locale> get supportedLocales => [localeZhCn, localeEnUs];

  // 支持的语言名称
  static List<I18nLocalName> get supportedName => [
        I18nLocalName("简体中文", localeZhCn),
        I18nLocalName("English", localeEnUs),
      ];

  static Locale get sysLocale => PlatformDispatcher.instance.locale;

  static late Locale _locale;

  static Locale get locale => _locale;

  // 当前语言对应支持的语言索引  不支持返回-1
  static int get currentLocaleIndex =>
      supportedLocales.indexWhere((it) => it.languageCode == I18nConfig.locale.languageCode);

  // static final Map<String, Map<String, String>> _translatedValues = {};

  static Future<void> init() async {
    // _translatedValues['en_US'] = await _load(Assets.translationsEnUS);
    // _translatedValues['zh_CN'] = await _load(Assets.translationsZhCN);
    _locale = getCacheLocale();
  }

  // 加载json文件
  // static Future<Map<String, String>> _load(String path) async {
  //   final jsonString = await rootBundle.loadString(path);
  //   final jsonMap = jsonDecode(jsonString) as Map<String, dynamic>;
  //   return Map<String, String>.fromEntries(jsonMap.entries.map((entry) {
  //     return MapEntry(entry.key, entry.value.toString());
  //   }));
  // }

  // 切换语言
  static toggleLanguage() {
    if (Get.locale == localeEnUs) {
      switchLanguage(localeZhCn);
    } else {
      switchLanguage(localeEnUs);
    }
  }

  // 切换语言
  static switchLanguage(Locale locale) {
    //这个是使用系统语言
    ///import 'dart:ui' as ui;
    //
    // return GetMaterialApp(
    //     locale: ui.window.locale,
    // );
    // if (Get.locale == Locale('en', 'US')) {
    _locale = locale;
    Get.updateLocale(locale);
    _saveThemePreference(locale);
    EventBusKey.LANGUAGE_CHANGE.busPost(locale);
  }

  // 保存语言偏好
  static Future<void> _saveThemePreference(Locale locale) async {
    if (locale == localeZhCn) {
      SpKey.locale.putStore(0);
    } else if (locale == localeEnUs) {
      SpKey.locale.putStore(1);
    }
  }

  //获取缓存
  static Locale getCacheLocale() {
    int localeIndex = SpKey.locale.getIntStore(def: -1);
    if (localeIndex != -1) {
      return [localeZhCn, localeEnUs][localeIndex];
    }
    //默认使用系统语言
    return sysLocale;
    // return localeEnUs;
  }
}
